package com.example.gcm;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
{
	   // boolean array for storing
	   //the state of each CheckBox 	   
	   private Context context;
	   private LayoutInflater inflater;
	   private ViewHolder viewHolder;
	   private ArrayList<HashMap<String, Object>> accounts;
	   private int i=0;
	   
	   public CustomAdapter(Context context, LayoutInflater inflater, int textViewResourceId, ArrayList<HashMap<String, Object>> accounts) 
	   {
	 
		    //let android do the initializing :)
		    super(context, textViewResourceId, accounts);
		    this.context= context;
		    this.inflater = inflater;
		    this.accounts = accounts;
		    
		    //create the boolean array with
		    //initial state as false
		    GCMActivity.checkBoxState=new boolean[accounts.size()];
		   
	  }
	 
	 
	    //class for caching the views in a row  
		 private class ViewHolder
		 {
		   ImageView photo;
		   TextView name,phone;
		   CheckBox checkBox;
		 }
		 
		   
		 
		 @Override
		 public View getView(final int position, View convertView, ViewGroup parent) {
		 
		   if(convertView==null)
		    {
			   convertView=inflater.inflate(R.layout.row, null);
			   viewHolder=new ViewHolder();
			 
			    //cache the views
			    viewHolder.photo=(ImageView) convertView.findViewById(R.id.imgAvatar);
			    viewHolder.name=(TextView) convertView.findViewById(R.id.tvName);
			    viewHolder.phone=(TextView) convertView.findViewById(R.id.tvPhone);
			    viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox1);
			 
			     //link the cached views to the convertview
			    convertView.setTag( viewHolder);
		    }
		  else
		   viewHolder=(ViewHolder) convertView.getTag();
		   
		  //int photoId=(Integer) accounts.get(position).get("photo");
		 
		  //set the data to be displayed
		  viewHolder.photo.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
		  viewHolder.name.setText(accounts.get(position).get("name").toString());
		  viewHolder.phone.setText(accounts.get(position).get("phone").toString());
		    
		   //VITAL PART!!! Set the state of the 
		   //CheckBox using the boolean array
		   viewHolder.checkBox.setChecked(GCMActivity.checkBoxState[position]);
		             
		          
		   //for managing the state of the boolean
		   //array according to the state of the
		   //CheckBox
		           
		   viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
		     
		   public void onClick(View v) 
		   {
		    if(((CheckBox)v).isChecked())
		     {
		    	GCMActivity.checkBoxState[position]=true;		    	
		    	Log.i("pos", position+"");
		     }
		    else
		    	GCMActivity.checkBoxState[position]=false;
		      
		    }
		   });
		 
		   //return the view to be displayed
		   return convertView;
		  }
	 
	 }